package edu.tms.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import edu.tms.bean.*;
import edu.tms.dao.*;
import net.easymvc.annotation.Encoding;
import net.easymvc.annotation.Param;
import net.easymvc.core.Action;


public class CadreAction extends Action{
	 //显示小组信息
	@Encoding("UTF-8")
	public String querySquadInfo(
			@Param("squadId")int squadId,
			@Param("pageNo")Integer pageNo) {
		if(pageNo == null || pageNo < 1) pageNo = 1;
		SquadDaoImp dao=new SquadDaoImp();
		try {
			//删除小组
			String sqId=request.getParameter("sqId");//接收小组ID
			if(sqId!=null){
				int sqIds=Integer.parseInt(sqId);
				dao.delete(sqIds);//删除小组
				//重查询一页信息
				List<Squad> squads=dao.findAll(squadId, pageNo);//获得一页的内容
				request.setAttribute("squad", squads);//传递的内容
				return "/s/cc/squad.jsp";//转到小组页面
			}
			String check=request.getParameter("check");//接收查询关键字
			if(check==null || check==""){//关键字为空
				//获取修改Id
				String qid=request.getParameter("qId");
				if(qid!=null){
					request.setAttribute("qqId", qid);
					int sqIds=Integer.parseInt(qid);
					List<Squad> squad=dao.findById(sqIds);//查询要修改小组id的内容
					request.setAttribute("squadId", squad);
				}
				List<Squad> squads=dao.findAll(squadId, pageNo);//获得一页的内容
				request.setAttribute("squad", squads);//传递的内容
				int pages=dao.tatolCount(pageNo);//总页数
				request.setAttribute("pages", pages);
				request.setAttribute("pageNo", pageNo);//当前页
				return "/s/cc/squad.jsp";//转到小组页面
			}else{//关键字不为空
				List<Squad> squads=dao.findAll(check, squadId, pageNo);//获得查询的内容
				request.setAttribute("squad", squads);//传递的内容
				return "/s/cc/squad.jsp";//转到小组页面
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	//小组详细信息
	@Encoding("UTF-8")
	public String xiaozu(){
		String id=request.getParameter("sqId");
		int idd=Integer.parseInt(id);
		SquadDaoImp dao=new SquadDaoImp();
		
		try {
			List<Squad> squad=dao.xiaozu(idd);
			request.setAttribute("sq", squad);//小组组长信息
			List<Student> s=dao.zuyuan(idd);
			request.setAttribute("st", s);//组员姓名
			return "/s/team.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "error.jsp";
		}
		
	}
	//小组详细信息
	@Encoding("UTF-8")
	public String xiaozus(){
		String id=request.getParameter("sqId");
		int idd=Integer.parseInt(id);
		System.out.println(idd);
		SquadDaoImp dao=new SquadDaoImp();
		
		try {
			List<Squad> squad=dao.xiaozu(idd);
			request.setAttribute("sqs", squad);//小组组长信息
			List<Student> s=dao.zuyuan(idd);
			request.setAttribute("st", s);//组员姓名
			return "/s/cc/squad.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "error.jsp";
		}
		
	}
	@Encoding("UTF-8")
	public String updateSquadInfo(Squad squad) {
		System.out.println(squad);
		return null;
	}
	@Encoding("UTF-8")
	public String queryAttendance(
			@Param("courseId")int courseId,
			@Param("pageNo") Integer pageNo) {
		if(pageNo == null || pageNo < 1) pageNo = 1;
		AttendanceDaoImp dao=new AttendanceDaoImp();//创建DAO
		String naId=request.getParameter("search");//输入的关键字
		CourseDaoImp courDao = new CourseDaoImp();
		try {
			//所有课程名称
			List<Course> cours=courDao.findAll();
			request.setAttribute("cours", cours);
			if(naId==null || naId==""){
				List<Attendance> kao=dao.findByCoursId(courseId,pageNo,PAGE_SIZE);
				request.setAttribute("kao", kao);//考勤内容
				request.setAttribute("cId", courseId);//课程id
				request.setAttribute("ni", naId);//数据回显
				int pages=dao.findByCoursIds(courseId,PAGE_SIZE);//总页数
				request.setAttribute("pages", pages);
			}else{
				List<Attendance> kao=dao.finByIdName(courseId, naId);
				request.setAttribute("kao", kao);//考勤内容
				request.setAttribute("cId", courseId);//课程id
				request.setAttribute("ni", naId);//数据回显
			}
			request.setAttribute("pageNo", pageNo);
			return "/s/cc/attendance.jsp";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 学习委员 成绩管理页
	 */
	@Encoding("UTF-8")
	public String queryStuGrade(
			@Param("examtype") String type,
			@Param("pageNo") Integer pageNo ,
			@Param("examname") String examname,
			@Param("search")String search ){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo==null || pageNo < 1)pageNo=1;
		ExamDaoImp exam = new ExamDaoImp();
		ExamTypeDaoImp examTypeDao = new ExamTypeDaoImp();
		GradeDaoImp dao=new GradeDaoImp();
		StudentDaoImp stuDao = new StudentDaoImp();
	    Integer totalPage=null;
		try {
			//获取用户所在班级的名称
	    	String className = stuDao.getClassName(user.getUserId());
	    	//给科目下拉框赋值
			List<Exam>course=exam.exams(className);
			setRequest("course", course);
			List<ExamType> examtype=examTypeDao.examtype(className);
			setRequest("examtypes",examtype);
			List<Grade> list = null;
			//查询全部
			if((type==null || type.equals("all")) && (examname==null || examname.equals("all"))&& (search==null ||search=="")){
				list = dao.findAllByClass(className,pageNo, PAGE_SIZE);
				//查询总页数
				totalPage=dao.totalPage(className,examname, PAGE_SIZE);
			}else if(search!=""){//查询某一学生所有成绩
				list = dao.findByClass(className, search, pageNo, PAGE_SIZE);
				totalPage=dao.totalPage(className,search, PAGE_SIZE);
			}else if(!examname.equals("all")){//根据考试科目查询
				list = dao.findByClass(className,examname, pageNo, PAGE_SIZE);
				totalPage=dao.totalPage(className,examname, PAGE_SIZE);
				
			}else if(!type.equals("all")){//根据考试类型项查询
				list = dao.findByClass(className,type, pageNo, PAGE_SIZE);
				totalPage=dao.totalPage(className,type, PAGE_SIZE);
		  	}
			//转发数据
			setRequest("gradelist", list);
			//转发总页数
			setRequest("totalPage", totalPage);
			//转发当前网页
			setRequest("pageNo", pageNo);
			//查询科目回显
			setRequest("search" , search);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return"/s/cc/search.jsp";
	}
	/**
	 * 学习委员 成绩统计页
	 */
	@Encoding("UTF-8")
	public String queryExamInfo(
			@Param("examtype") String type,
			@Param("examname") String ename,
			@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo==null || pageNo < 1)pageNo=1;
		Integer totalPage=null;
		ExamDaoImp dao=new ExamDaoImp();
		ExamTypeDaoImp examTypeDao = new ExamTypeDaoImp();
		StudentDaoImp stuDao = new StudentDaoImp();
		try {
			//获取用户所在班级的名称
	    	String className = stuDao.getClassName(user.getUserId());
	    	//给科目下拉列表赋值
			List<Exam> exams=dao.exams(className);
			setRequest("exams",exams);
			List<ExamType> examtype=examTypeDao.examtype(className);
			setRequest("examtypes",examtype);
			List<Exam> tongji=null;
			if((ename==null || ename.equals("all")) && (type==null || type.equals("all"))){
				tongji=dao.findAllByClass(className,pageNo, PAGE_SIZE);
				//计算总页数
				totalPage=dao.totalPage(className,PAGE_SIZE);
			}else if(!ename.equals("all")){
				//根据科目名称查询内容
				tongji=dao.find(className,ename);
			}else if(!type.equals("all")){
				//根据科目类型查询内容
				 tongji=dao.find(className,type);
			}
			setRequest("tongji",tongji);
			//转发当前网页
			setRequest("pageNo", pageNo);
			//转发总页数
			setRequest("totalPage", totalPage);
			return"/s/cc/socercount.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return"/error.jsp";
		}
	}
	
	//计算总页数
	@SuppressWarnings("rawtypes")
	public static int totalpage(List l,Integer pageSize){
		int page=0;
		if(l.size()%pageSize==0){
			page=l.size()/pageSize;
		}else{
			page=l.size()/pageSize+1;
		}
		return page;
	}
	/**
	 * 学习委员 作业统计页
	 */
	@Encoding("UTF-8")
	public String queryHomeworkCount(
			@Param("major") String type,
			@Param("pageNo") Integer pageNo ,
			@Param("coursename") String coursename ){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo==null || pageNo < 1)pageNo=1;
		HomeworkDaoImp dao=new HomeworkDaoImp();
		StudentDaoImp stuDao = new StudentDaoImp();
	    Integer totalPage=null;
	    try {
	    	//获取用户所在班级的名称
	    	String className = stuDao.getClassName(user.getUserId());
	    	//给科目下拉框传值
	    	List<Homework> list = dao.worknames(className);
	    	setRequest("list",list);
	    	List<Homework> works = null;
	    	//查询全部
	    	if((type==null || type.equals("all")) && (coursename==null ||coursename.equals("all"))){
			works=dao.findDatumByClass(className, pageNo, PAGE_SIZE);
	    	}else if(type!=null && !type.equals("all")){
	    		//根据专业名查询
	    		works=dao.findDatumByName(className,type, pageNo, PAGE_SIZE);
	    	}else if(coursename!=null && !coursename.equals("all")){
	    		//根据课程名查询
	    		works=dao.findDatumByName(className,coursename, pageNo, PAGE_SIZE);
	    	}
	    	setRequest("worklist", works);
    		//计算总页数
    		totalPage=totalpage( works, PAGE_SIZE);
			//转发当前网页
    		setRequest("pageNo", pageNo);
			//转发总页数
    		setRequest("totalPage", totalPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return"/s/cc/homeworkcount.jsp";
	}
	
	
	/**
	 * 组长 组员管理查看
	 * @param squadId
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String queryStuSquad(
			@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//添加对应的dao
		StudentDaoImp stuDao = new StudentDaoImp();
		SquadDaoImp dao = new SquadDaoImp();
		try {
			int squadId = stuDao.getSquadId(user.getUserId());
			//将查询出来的内容保存在集合里
			List<Squad> squad = dao.findAll(squadId, pageNo, PAGE_SIZE);
			//发送到网页
			setSession("squads", squad);
			//返回 到具体的网页
			return"/s/cc/member.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 组长 组员作业信息
	 * @param homeWorkId
	 * @param squadId
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String queryStuSquadHomework(
			@Param("homeWorkId") Integer homeWorkId,
			@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		StudentDaoImp stuDao = new StudentDaoImp();
		//添加对应的dao
		HomeworkDaoImp dao = new HomeworkDaoImp();
		try {
			int squadId = stuDao.getSquadId(user.getUserId());
			int pages = (int)dao.getPages(PAGE_SIZE);
			if(pages > 1 && pageNo > pages) pageNo = pages;
			//将查询出来的内容保存在集合里
			List<Homework> Homework = dao.findBySquad(squadId, pageNo, PAGE_SIZE);
			//将数据发送到网页
			setRequest("Homework", Homework);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			//返回 到具体的网页
			return"/s/cc/memberWork.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 组长 按条件查询组员作业信息
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String findByWork(@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		StudentDaoImp stuDao = new StudentDaoImp();
		//添加对应的dao
		HomeworkDaoImp dao = new HomeworkDaoImp();
		//获取查询所需条件
		String name = getRequest().getParameter("findByname");
		try {
			int squadId = stuDao.getSquadId(user.getUserId());
			int pages = (int)dao.getPages(PAGE_SIZE);
			if(pages > 1 && pageNo > pages) pageNo = pages;
			if(name == "" || name == null){//判断条件是否为空
				List<Homework> Homework1 = dao.findBySquad(squadId, pageNo, PAGE_SIZE);
				setSession("Homework", Homework1);
			}else{//条件不为空
				if(pages<PAGE_SIZE)pages=1;
				List<Homework> Homework2 = dao.findByStuName(name, squadId, pageNo, PAGE_SIZE);
				setSession("Homework", Homework2);
			}
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			return"/s/cc/memberWork.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 组长 组员考勤信息
	 * @param checkNo
	 * @param squadNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String queryStuSquadCheck(
			@Param("checkNo") Integer checkNo,
			@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//添加对应的dao
		StudentDaoImp stuDao = new StudentDaoImp();
		AttendanceDaoImp dao = new AttendanceDaoImp();
		try {
			int squadId = stuDao.getSquadId(user.getUserId());
			int pages = (int)dao.getPages(PAGE_SIZE);
			if(pages > 1 && pageNo > pages) pageNo = pages;
			List<Attendance> Attendance = dao.findBySquad(squadId, pageNo, PAGE_SIZE);
			//发送到网页
			setRequest("Attendance", Attendance);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			//返回 到具体的网页
			return"/s/cc/memberCourse.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 组长 按条件查询组员考勤信息
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String findBySome(@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//添加对应的dao
		StudentDaoImp stuDao = new StudentDaoImp();
		AttendanceDaoImp dao = new AttendanceDaoImp();
		//获取查询所需条件
		String name = getRequest().getParameter("findByname");
		try {
			int squadId = stuDao.getSquadId(user.getUserId());
			int pages = (int)dao.getPages(PAGE_SIZE);
			if(pages > 1 && pageNo > pages) pageNo = pages;
			if(name == "" || name == null){//判断条件是否为空
				List<Attendance> Attendance1 = dao.findBySquad(squadId, pageNo, PAGE_SIZE);
				setSession("Attendance", Attendance1);
			}else{//条件不为空
				if(pages<PAGE_SIZE)pages=1;
				List<Attendance> Attendance2 = dao.findAll(name, squadId, pageNo, PAGE_SIZE);
				setSession("Attendance", Attendance2);
			}
			setSession("pageNo", pageNo);
			setSession("pages", pages);
			return"/s/cc/memberCourse.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 宣传委员 添加活动信息以及创建空相册
	 * @param classActive
	 * @return
	 */
	@Encoding("utf-8")
	public String appendClassActive(ClassActivity classActive) {
		//实例化DAO
		ClassActivityDaoImp cad = new ClassActivityDaoImp();
		AlbumDaoImp albumDao = new AlbumDaoImp();
		try {
			//设置班级ID
			classActive.setClassId(1);
			//新建一个空相册
			Album album = new Album();
			//根据活动标题以及id创建文件名
			album.setAlbumName(classActive.getActivityTitle()+classActive.getClassActivityId());
			//TODO 
			album.setAlbumUrl("download/");
			album.setAlbumState(1);
			album.setCreateTime(new Date());
			//新建相册
			File file = new File(album.getAlbumUrl());
			if(!file.exists()) file.mkdirs();
			//保存空相册
			Long albumId = new Long((long)0);
			if(file.exists()) albumId = albumDao.save(album);
			//设置相册ID
			classActive.setAlbumId(albumId.intValue());
			//保存
			cad.save(classActive);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:cadre/classActivity.do";
	}
	
	
	/**
	 * 查询活动信息用于页面展示
	 * @param classNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("UTF-8")
	public String activityState(
			@Param("id")Integer id,
			@Param("state") Integer state) {
		//实例化DAO
		ClassActivityDaoImp dao = new ClassActivityDaoImp();
		try {
			System.out.println("id="+id+",state="+state);
			if(dao.changeState(id, state) == 0){
				System.out.println("not");
				setRequest("error_msg", "信息递交失败请重试");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cadre/classActivity.do";
	}
	
	/**
	 * 宣传委员
	 * 查询活动信息用于页面展示
	 * @param classNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("UTF-8")
	public String classActivity(
			@Param("pageNo") Integer pageNo) {
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//dao
		StudentDaoImp stuDao = new StudentDaoImp();
		ClassActivityDaoImp dao = new ClassActivityDaoImp();
		try {
			//获取本班id
			int classId = stuDao.getClassId(user.getUserId());
			//创建数组接收查询到的数据
			List<ClassActivity> ca = dao.findAll(classId, pageNo, 5);
			//遍历数组ca 用于获取CreateTime
			for(ClassActivity cc:ca){
				//查询对应实体的CreateTime,用cd来存储只有CreateTime的实体类
				List<ClassActivity> cd = dao.findAllCreateTime(cc.getClassActivityId());
				//遍历数组cd 用于把CreateTime存入实体类cc中
				for(ClassActivity ch:cd){
					//把获取到的CreateTime存入
					cc.setCreateTime(ch.getCreateTime()); 
				}
			}
			//获取总页数
			Long pages = dao.getPages(classId,5);
			if( pageNo > pages) pageNo = pages.intValue();
			System.out.println(pageNo);
			//设置网页属性
			setRequest("ClassActivity",ca);
			setRequest("pages",pages);
			setRequest("pageNo", pageNo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/s/cc/activ.jsp";
	}
	
	/**
	 * 查询报名信息用于页面展示
	 * @param classNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("UTF-8")
	public String activityEnroll(
			@Param("pageNo") Integer pageNo) {
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//创建数组接收查询到的数据
		StudentDaoImp stuDao = new StudentDaoImp();
		ActivityEnrollDaoImp aed = new ActivityEnrollDaoImp();
		try {
			//获取本班id
			int classId = stuDao.getClassId(user.getUserId());
			List<ActivityEnroll> ae = aed.findAll(classId, pageNo, 8);
			//获取总页数
			Long pages = aed.getPages(classId, 8);
			if( pageNo > pages) pageNo = pages.intValue();
			//设置网页属性
			setRequest("ae", ae);
			setRequest("pages",pages);
			setRequest("pageNo", pageNo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/s/cc/enroll.jsp";
	}
	
	/**
	 * 通过或拒绝通过审核操作
	 */
	@Encoding("utf-8")
	public String enrollState(
			@Param("id")Integer id,
			@Param("state")Integer state) {
		if(id == null || state == null) setRequest("error_msg", "信息递交失败，请重试");
		//创建数组接收查询到的数据
		ActivityEnrollDaoImp aed = new ActivityEnrollDaoImp();
		try {	
			aed.updateState(id, state);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "cadre/activityEnroll.do";
	}
	
	/**
	 * 添加学生经历
	 * @return
	 */
	@Encoding("utf-8")
	public String appendSchoolLife(SchoolLife schoollife) {
		//实例化DAO
		AlbumDaoImp albumDao = new AlbumDaoImp();
		SchoolLifeDaoImp sld = new SchoolLifeDaoImp();
		PictureDaoImp pd = new PictureDaoImp();
		try {
			//新建一个空相册
			Album album = new Album();
			//根据活动标题以及id创建文件名
			album.setAlbumName(schoollife.getActivityTitle()+schoollife.getSchoolLifeId());
			album.setAlbumUrl("download/");
			album.setAlbumState(1);
			album.setCreateTime(new Date());
			//TODO 新建相册文件
			File file = new File(album.getAlbumUrl());
			if(!file.exists()) file.mkdirs();
			//保存空相册
			Long albumId = new Long((long)0);
			if(file.exists()) albumId = albumDao.save(album);
			//设置相册ID
			schoollife.setAlbumId(albumId.intValue());
			//保存学生经历
			sld.save(schoollife);
			//创建变量接受文件路径
			String xfile = null;
			//遍历往数组添加路径 最多为8个
			for (int i = 0; i < 7; i++) {
				//new一个相片实体类
				Picture p = new Picture();
				xfile = request.getParameter("xfile"+(i+1));
				if(xfile!=null && xfile!=""){
					//设置属性
					p.setAlbumId(albumId.intValue());
					p.setPictureName(schoollife.getActivityTitle());
					p.setPictureUrl(xfile);
					//保存图片
					pd.save(p);
				}
			}
			return "/s/cc/course.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	/**
	 * 团支书 查询申请通过的入党的学生
	 */
	@Encoding("utf-8")
	public String queryApplicationInfo(
			@Param("pageNo")Integer pageNo) {
		//显示所有本班学生
		//获取到登录的学生的信息，并找到他的班级号
		//根据班级号查询学生
		//获取到登录的用户
		User user= (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		//获取登录的用户的id
		int uid = user.getUserId();
		//当获取到的pageNo为空时，设置为1
		if(pageNo == null || pageNo < 1) pageNo=1;
		//创建一个StudentDaoImp
		StudentDaoImp dao = new StudentDaoImp();
		try {
			//查到对应的学生
			Student stu = dao.findByKey(uid);
			//获取到同一个班的学生
			List<Student> stus= dao.findAllApplication(stu.getStuClassName(),pageNo, PAGE_SIZE);
			//计算查找到的可以分为几页
			int size = (int) dao.getCountWithMember(stu.getStuClassName(),PAGE_SIZE);
			//将查出的学生传给网页
			setRequest("stus",stus);
			//将当前页数传给网页
			setRequest("pageNo",pageNo);
			//将总共的页数传回网页
			setRequest("size",size);
			//返回网页路径
			return "/s/cc/partyMember.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "/err.html";
	}
	
	
	/**
	 * 团支书 查看提交的入党申请
	 */
	@Encoding("utf-8")
	public String apply(
			@Param("pageNo")Integer pageNo) {
		//获取到登录的用户
		User user=(User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		int uid = user.getUserId();
		//当获取到的pageNo为空时，设置为1
		if(pageNo == null || pageNo < 1) pageNo=1;
		//创建一个ApplicationInfoDaoImp
     	ApplicationInfoDaoImp appDao=new ApplicationInfoDaoImp();
     	//创建一个StudentDaoImp
     	StudentDaoImp stuDao=new StudentDaoImp();
     	try {
     		//查到对应的学生
     		Student stu = stuDao.findByKey(uid);
     		//获取到同一个班的学生
     		List<ApplicationInfo> alist = appDao.findAllApplication(stu.getStuClassName(),pageNo, PAGE_SIZE);
			//计算查找到的可以分为几页
     		int size=(int)appDao.getCount(stu.getStuClassName(),PAGE_SIZE);
     		//将查出的学生传给网页
     		setRequest("size",size);
     		//将当前页数传给网页
     		setRequest("application",alist);
     		//将总共的页数传回网页
     		setRequest("pageNo",pageNo);
     		//返回网页路径
     		return "/s/cc/apply.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/err.html";
		}
	}
	
	/**
	 * 团支书 同意/拒绝入党申请
	 * @param pid 
	 * @return
	 */
	public String applyState(
			@Param("pid")Integer pid,
			@Param("applyState") Integer state){
		if(pid == null || state == null){
			setRequest("error_msg", "信息递交错误，请重试");
			return "ajax:forword:/cadre/apply.do";
		}
		ApplicationInfoDaoImp dao=new ApplicationInfoDaoImp();
     	try {
			if(dao.updateState(pid,state) == 0)
				setRequest("error_msg", "信息更新错误，请重试");
			return "ajax:forword:/cadre/apply.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
     	return "ajax:forword:/cadre/apply.do";
	}
	
	
	/**
	 * 团支书 修改学生党校是否完成
	 * @param sid 学生id
	 * @return 网页
	 * @throws Exception 
	 */
	public String partyStateCompleted(@Param("sid")Integer sid) {
		if(sid == null){
			setRequest("error_msg", "信息递交错误，请重试");
			return "ajax:forword:/cadre/queryApplicationInfo.do";
		}
		//创建一个ApplicationInfoDaoImp
     	ApplicationInfoDaoImp dao = new ApplicationInfoDaoImp();
     	try {
			if(dao.stateCompleted(sid) == 0)
				setRequest("error_msg", "信息更新失败，请重试");
			return "ajax:forword:/cadre/queryApplicationInfo.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
     	return "ajax:forword:/cadre/queryApplicationInfo.do";
	}
	
	/**
	 * 生活委员 查询本班财务明细
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String classfee(@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		StudentDaoImp stuDao = new StudentDaoImp();
		ClassFeeDaoImpl dao = new ClassFeeDaoImpl();
		try {
			//获取学生活委员所在的班级id
			int classId = stuDao.getClassId(user.getUserId());
			long pages = dao.getCount(classId,10);
			if(pageNo > (int)pages) pageNo = (int) pages;
			//通过班级id获取生活委员所在的班级的收支明细
			List<ClassFee> list = dao.findByClassId(classId, pageNo, PAGE_SIZE);
			setRequest("classfees", list);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			return "/s/cc/finance.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/s/cc/finance.jsp";
	}
	
	/**
	 * 生活委员 增加本班财务明细
	 * @param classfee
	 * @return
	 */
	@Encoding("utf-8")
	public String addFee(ClassFee classfee){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		StudentDaoImp stuDao = new StudentDaoImp();
		ClassFeeDaoImpl dao = new ClassFeeDaoImpl();
		try {
			//获取学生活委员所在的班级id
			int classId = stuDao.getClassId(user.getUserId());
			classfee.setClassId(classId);
			//设置操作人
			classfee.setUserId(user.getUserId());
			classfee.setCreateTime(new Date());
			if(classfee.getClassFeeState() == 1)
				classfee.setTotal(dao.getTotal(classId) + classfee.getMoney());
			else if(classfee.getClassFeeState() == 2)
				classfee.setTotal(dao.getTotal(classId) - classfee.getMoney());
			else return "ajax:请选择收支";
			if(dao.save(classfee) != 0){
				return "ajax:forward:cadre/classfee.do";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ajax:forward:cadre/classfee.do";
	}
	
	/**
	 * 添加考勤
	 * @param month 月
	 * @param years 年
	 * @param dormState 状态:优秀、不优秀
	 * @param dormId 寝室id
	 * @return
	 */
	@Encoding("utf-8")
	public String addDormCheck(
			@Param("month") Integer month,
			@Param("period") String years,
			@Param("dormState") Integer dormState,
			@Param("dormId") Integer dormId
			){
		//获取到年号
		String year=years.substring(0, 4);
		//获取用户
		User user = (User) getSession("curUser");
		//没有用户就返回登录页面
		if(user == null)return "redirect:/login.html";
		//创建一个DormCheckDaoImp类
		DormCheckDaoImp drcDao=new DormCheckDaoImp();
		try{
			//当没有选择状态返回
			if(dormState==0){
				return "ajax:请选择等级";
			}else{
				//否则保存数据
			DormCheck dormCheck=new DormCheck();
			dormCheck.setCreateTime(new Date());
			dormCheck.setCheckMonth(month);
			dormCheck.setCheckTerm(year);
			dormCheck.setDormState(dormState);
			dormCheck.setDormId(dormId);
			drcDao.save(dormCheck);
			return  "cadre/dormAttendance.do";
			}
		}catch (Exception e){
			return  "cadre/dormAttendance.do";
		}
	}
	/**
	 * 查询班级所有寝室的所有考勤记录
	 * @param pageNo 页数
	 * @return 执行后的网页
	 */
	@Encoding("utf-8")
	public String dormAttendance(
			@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		ClassDormDaoImpl dromDao = new ClassDormDaoImpl();
		StudentDaoImp stuDao = new StudentDaoImp();
		DormCheckDaoImp dcDao=new DormCheckDaoImp();
		try {
			//通过学生获取到他的班级id
			int classId = stuDao.getClassId(user.getUserId());	
			//查询该班的所有寝室，得到的是寝室号和寝室名
			List<ClassDorm> dorm=dromDao.findAll(classId);			
			//查看所有的考勤记录
			List<DormCheck> dormCheck=dcDao.findAll(classId, pageNo, PAGE_SIZE);
			//计算出共有多少页
			Long pages = dcDao.getCount(classId,PAGE_SIZE);
			//给pageNo赋值
			if(pageNo > pages.intValue()) pageNo = pages.intValue();
			//将寝室号，名传到网页
			setRequest("dorms", dorm);
			//将寝室考勤传到网页
			setRequest("dormCheck", dormCheck);
			//将当前页数传到网页
			setRequest("pageNo", pageNo);
			//将总页数传到网页
			setRequest("pages", pages);
			return "/s/cc/bedroom.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/s/cc/bedroom.jsp";
		}
	}
	
	/**
	 * 查看本班的宿舍
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String dorms(@Param("pageNo") Integer pageNo){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		ClassDormDaoImpl dromDao = new ClassDormDaoImpl();
		StudentDaoImp stuDao = new StudentDaoImp();
		try {
			//或得班级id
			int classId = stuDao.getClassId(user.getUserId());		
			//查询班级的所有寝室
			List<ClassDorm> drom=dromDao.findClassDrom(classId, pageNo, PAGE_SIZE);
			//计算页数
			Long pages = dromDao.getCount(classId,PAGE_SIZE);
			if(pageNo > pages.intValue()) pageNo = pages.intValue();
			setRequest("drom", drom);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			return "/s/cc/classdorm.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/s/cc/bedroom.jsp";
		}
	}

	
}